namespace AltNetUK.Models
{
    using System;
    using System.Globalization;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot(ElementName = "utc")]
    public sealed class UtcDateTime
    {
        public static implicit operator string(UtcDateTime utc)
        {
            return (null == utc) ? null as string : utc.Value;
        }

        public static implicit operator UtcDateTime(string value)
        {
            return (null == value) ? null as UtcDateTime : new UtcDateTime() { Value = value };
        }

        [XmlText]
        public string Value
        {
            get;
            set;
        }

        [XmlIgnore]
        public string Date
        {
            get
            {
                DateTime value = XmlConvert.ToDateTime(this.Value, XmlDateTimeSerializationMode.Local);
                return string.Format(
                    CultureInfo.InvariantCulture, 
                    "{0} {1}",
                    value.DayOfWeek.ToString("G"),
                    value.Date.ToString("d MMMM yyyy", new CultureInfo("en-GB")));
            }
        }

        [XmlIgnore]
        public string Time
        {
            get
            {
                return XmlConvert.ToDateTime(this.Value, XmlDateTimeSerializationMode.Local).ToShortTimeString();
            }
        }

        [XmlIgnore]
        public string Calendar
        {
            get
            {
                return XmlConvert.ToString(this.ToDateTime(), XmlDateTimeSerializationMode.Utc).Replace("-", string.Empty).Replace(":", string.Empty);
            }
        }

        public DateTime ToDateTime()
        {
            return XmlConvert.ToDateTime(this.Value, XmlDateTimeSerializationMode.Local);
        }
    }
}